package com.pxl.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pxl.springbootinit.common.BaseResponse;
import com.pxl.springbootinit.common.ResultUtils;
import com.pxl.springbootinit.mapper.ItemMapper;
import com.pxl.springbootinit.mapper.TaskMapper;
import com.pxl.springbootinit.mapper.UserMapper;
import com.pxl.springbootinit.model.entity.User;
import com.pxl.springbootinit.model.vo.StatisticsAdmin;
import com.pxl.springbootinit.model.vo.StatisticsCharts;
import com.pxl.springbootinit.model.vo.StatisticsItemTypeVO;
import com.pxl.springbootinit.service.ItemService;
import com.pxl.springbootinit.service.TaskService;
import com.pxl.springbootinit.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 后管统计接口
 *
 * @author MADAO
 * @create 2024 - 12 - 24 22:35
 */
@RestController
public class StatisticsController {

    @Resource
    private UserMapper userMapper;
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private TaskMapper taskMapper;


    @GetMapping("/statistics/latest")
    public BaseResponse<StatisticsAdmin> statistics() {
        StatisticsAdmin statisticsAdmin = new StatisticsAdmin();
        // todo：待优化，并排查询 或 加缓存
        statisticsAdmin.setTotalItem(itemMapper.selectCount(null));
        statisticsAdmin.setTotalTask(taskMapper.selectCount(null));
        statisticsAdmin.setTotalUser(userMapper.selectCount(null));
        statisticsAdmin.setTodayActiveUserNum(userMapper.selectCount(new QueryWrapper<User>().eq("active_status",1)));

        return ResultUtils.success(statisticsAdmin);
    }

    @GetMapping("/statistics/charts")
    public BaseResponse<StatisticsCharts> charts() {
        StatisticsCharts statisticsCharts = new StatisticsCharts();
        statisticsCharts.setItemAddList(itemMapper.getRecent7DayAddCount());
        statisticsCharts.setTaskAddList(taskMapper.getRecent7DayAddCount());
        statisticsCharts.setUserAddList(userMapper.getRecent7DayAddCount());

        return ResultUtils.success(statisticsCharts);
    }


    @GetMapping("/statistics/item-group")
    public BaseResponse<List<StatisticsItemTypeVO>> itemGroup() {
        return ResultUtils.success(itemMapper.getItemGroupType());
    }
}
